package com.ruoyi.community.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.community.domain.BaseActivityRegisterRequest;
import com.ruoyi.community.domain.OwnerActivity;
import com.ruoyi.community.mapper.OwnerActivityParticipateMapper;
import com.ruoyi.community.mapper.OwnerActivityMapper;
import com.ruoyi.community.service.IOwnerActivityParticipateService;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.community.domain.OwnerActivityParticipate;

/**
 * 活动签到详情Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-12-13
 */
@Service
public class OwnerActivityParticipateServiceImpl implements IOwnerActivityParticipateService
{
    @Autowired
    private OwnerActivityParticipateMapper ownerActivityParticipateMapper;

    @Autowired
    private OwnerActivityMapper ownerActivityMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public OwnerActivityParticipate selectOwnerActivityParticipateById(String id)
    {
        return ownerActivityParticipateMapper.selectOwnerActivityParticipateById(id);
    }

    @Override
    public List<OwnerActivityParticipate> selectOwnerActivityParticipateList(OwnerActivityParticipate ownerActivityParticipate)
    {
        return ownerActivityParticipateMapper.selectOwnerActivityParticipateList(ownerActivityParticipate);
    }

    @Override
    public List<SysUser> selectOwnerActivityParticipateListById(String id)
    {
        List<OwnerActivityParticipate> listEnroll= ownerActivityParticipateMapper.selectOwnerActivityParticipateListById(id);
        List<SysUser> listUser = new ArrayList<>();
        SysUser query = new SysUser();
        for (OwnerActivityParticipate ownerActivityParticipate : listEnroll) {
            query.setUserId(ownerActivityParticipate.getUserId());
            query.setCommunityId(SecurityUtils.getCommunityId());
            List<SysUser> participateUsers= sysUserMapper.selectUserOnlyOnUser(query);
            listUser.addAll(participateUsers);
        }
        return listUser;
    }

    @Override
    public int insertOwnerActivityParticipate(BaseActivityRegisterRequest baseActivityRegisterRequest)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        int hasRegistered = ownerActivityParticipateMapper.hasRegistered(loginUser.getUserId().toString(), baseActivityRegisterRequest.getActivityId());
        if(hasRegistered == 1){
            return -1;
        }else{
            OwnerActivity ownerActivity = ownerActivityMapper.selectOwnerActivityById(baseActivityRegisterRequest.getActivityId());
            Long realRegisterCode = ownerActivity.getRegisterCode();
            if (!baseActivityRegisterRequest.getRegisterCode().equals(realRegisterCode)){
                return -999;
            }else {
                OwnerActivityParticipate ownerActivityParticipate = new OwnerActivityParticipate();
                ownerActivityParticipate.setUserId(loginUser.getUserId());
                ownerActivityParticipate.setActivityId(baseActivityRegisterRequest.getActivityId());
                ownerActivityParticipate.setTime(baseActivityRegisterRequest.getTime());

                Long participateNumber = ownerActivity.getParticipateNumber() + 1;
                ownerActivity.setParticipateNumber(participateNumber);
                ownerActivityMapper.updateOwnerActivity(ownerActivity);

                return ownerActivityParticipateMapper.insertOwnerActivityParticipate(ownerActivityParticipate);
            }
        }
//        OwnerActivity ownerActivity = ownerActivityMapper.selectOwnerActivityById(baseActivityRegisterRequest.getActivityId());
//        Long participateNumber = ownerActivity.getParticipateNumber() + 1;
//        ownerActivity.setParticipateNumber(participateNumber);
//        ownerActivityMapper.updateOwnerActivity(ownerActivity);
//        return ownerActivityParticipateMapper.insertOwnerActivityParticipate(ownerActivityParticipate);
    }

    /**
     * 查询是否已经签到
     *
     * @param baseActivityRegisterRequest 活动签到详情
     * @return 结果
     */
    @Override
    public boolean hasRegistered(BaseActivityRegisterRequest baseActivityRegisterRequest)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        int hasRegistered = ownerActivityParticipateMapper.hasRegistered(loginUser.getUserId().toString(), baseActivityRegisterRequest.getActivityId());
        return hasRegistered==1;
    }

    /**
     * 修改活动签到详情
     * 
     * @param ownerActivityParticipate 活动签到详情
     * @return 结果
     */
    @Override
    public int updateOwnerActivityParticipate(OwnerActivityParticipate ownerActivityParticipate)
    {
        return ownerActivityParticipateMapper.updateOwnerActivityParticipate(ownerActivityParticipate);
    }

    /**
     * 批量删除活动签到详情
     * 
     * @param ids 需要删除的活动签到详情ID
     * @return 结果
     */
    @Override
    public int deleteOwnerActivityParticipateByIds(String[] ids)
    {
        return ownerActivityParticipateMapper.deleteOwnerActivityParticipateByIds(ids);
    }

    /**
     * 删除活动签到详情信息
     * 
     * @param id 活动签到详情ID
     * @return 结果
     */
    @Override
    public int deleteOwnerActivityParticipateById(String id)
    {
        return ownerActivityParticipateMapper.deleteOwnerActivityParticipateById(id);
    }
}
